<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/static/css/custom.css}">
    <script th:src="@{/static/layui/layui.js}"></script>
    <script th:src="@{/static/js/coreutil.js}"></script>
</head>
<body>
<!--搜索框-->
<div class="table_div" style="margin-left: 7px;margin-right: 7px">
    <div id="searchParam">
        <div class="layui-form-item">

            <div class="layui-inline" style="margin-top: 0;margin-right: 0px;margin-bottom: 0px;">
                <label class="layui-form-label" style="padding-left: 0px">用户ID</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" id="userId1" placeholder="请输入用户ID"
                           type="text">
                </div>
            </div>
            <div class="layui-inline" style="margin-top: 0;margin-right: 0px;margin-bottom: 0px;">
                <label class="layui-form-label" style="padding-left: 0px">角色ID</label>
                <div class="layui-input-inline">
                    <input autocomplete="off" class="layui-input" id="roleId1" placeholder="请输入角色ID"
                           type="text">
                </div>
            </div>
            <div class="layui-inline" style="margin-top: 5px">
                <div class="layui-input-inline" style="margin-top: 5px">
                    <button class="layui-btn" id="search" onclick="search()">查询</button>
                    <button class="layui-btn" id="export">导出全部</button>
                </div>
            </div>
        </div>

    </div>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>
</div>
<script id="toolbar" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted">删除
        </button>
    </div>
</script>
<script id="tool" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--弹出层 添加-->
<div class="site-text" id="add" style="margin: 5%; display: none" target="test123">
    <form action="" class="layui-form " lay-filter="info" style="width: 700px;margin-top: 10px">
        <input hidden name="userId"/>
        <div class="layui-form-item">
            <label class="layui-form-label">角色ID</label>
            <div class="layui-input-block">
                <input autocomplete="off" class="layui-input" name="roleId"
                       placeholder="请输入角色ID" type="text">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="submit" lay-submit="" type="submit">保存</button>
                <button class="layui-btn layui-btn-primary" id="btn_cancel">取消</button>
            </div>
        </div>
    </form>
</div>
<!--弹出层 修改-->
<div class="site-text" id="up" style="margin: 5%; display: none" target="test123">
    <form action="" class="layui-form " lay-filter="info" style="width: 700px;margin-top: 10px">
        <div class="layui-form-item">
            <label class="layui-form-label">用户ID</label>
            <div class="layui-input-block">
                <input autocomplete="off" class="layui-input" disabled="disabled" id="userId"
                       name="userId"
                       placeholder="请输入用户ID" style="background:#CCCCCC" type="text">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色ID</label>
            <div class="layui-input-block">
                <input autocomplete="off" class="layui-input" id="roleId"
                       name="roleId" placeholder="请输入角色ID" type="text">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="submit" lay-submit="" type="submit">保存</button>
                <button class="layui-btn layui-btn-primary" id="btn_cancel1">取消</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>

<script th:inline="javascript">
    var ctx = [[@{/}]];
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;

    layui.use(['table', 'layer', 'laydate'], function () {

        //加载table
        tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            // , headers: {"authorization": token}
            , url: ctx + 'sysUserRole/listByPage' //数据接口
            , method: 'POST'
            , limit: 15
            , limits: [15, 25, 50, 100]
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'userId', title: '用户ID', sort: true},
                    {field: 'roleId', title: '角色ID', sort: true},
                    {width: 120, toolbar: "#tool", title: '操作'}
                ]
            ]
            , page: {
                layout: ['prev', 'page', 'next', 'refresh', 'skip', 'count', 'limit']
                , first: '首页' //不显示首页
                , last: '尾页' //不显示尾页
                , groups: 3 //显示 3个连续页码
            }
            , toolbar: '#toolbar'
        });


        //表头工具
        table.on('toolbar(showTable)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的列");
                    } else {
                        var ids = [];
                        $(data).each(function (index, item) {
                            ids.push(item.user_id);       //更换成表的id
                        });
                        tipDialog(ids);
                    }
                    break;
                case 'add':
                    layer.open({
                        type: 1,
                        title: "添加",
                        area: ["800px", "600px"],

                        // btn: ['提交', '取消'], //可以无限个按钮
                        Number: 9000000,   //自动关闭的时间
                        maxmin: true, // 最大最小化。
                        resize: true, // 是否允许拉伸
                        anim: 2,
                        content: $('#add'), //添加表单到弹出层
                    })
                    break;
            }
            ;
        });
        //列操作
        table.on('tool(showTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var ids = [];
                    ids.push(data.user_id);   //更换成表的id
                    tipDialog(ids);
                    break;
                case 'edit':

                    document.getElementById("userId").value = data.userId;
                    document.getElementById("roleId").value = data.roleId;

                    layer.open({
                        type: 1,
                        title: "修改",
                        area: ["800px", "600px"],

                        // btn: ['提交', '取消'], //可以无限个按钮
                        Number: 9000000,   //自动关闭的时间
                        maxmin: true, // 最大最小化。
                        resize: true, // 是否允许拉伸
                        anim: 2,
                        content: $('#up'), //添加表单到弹出层

                    })
                    break;
            }
        });

        //导出
        $('#export').on('click', function () {
            //原先分页limit
            var exportParams = {
                limit: 10000,
                key: $("#key").val()
            };
            let url = ctx + "sysUserRole/listByPage"
            coreUtilsendPost(url, exportParams, function (res) {
                //初始化渲染数据
                if (res.data != null && res.data.records != null) {
                    table.exportFile(tableIns1.config.id, res.data.records, 'xls');
                }
            });
        });

        //删除
        var tipDialog = function (ids) {
            layer.open({
                content: "确定要删除么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    let url = ctx + "sysUserRole/delete"
                    coreUtilsendDelete(url, ids, function (res) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            search();
                        });
                    });
                }
            });
        };

        //返回
        $("#btn_cancel").click(function () {
            layer.closeAll(); //关闭所有弹窗
            return false;
        });
        //返回
        $("#btn_cancel1").click(function () {
            layer.closeAll(); //关闭所有弹窗
            return false;
        });

        //监听保存
        form.on('submit(submit)', function (data) {
            if (data.field.user_id === undefined || data.field.user_id === null || data.field.user_id === "") {     //更换成表的id
                let url = ctx + "sysUserRole/add";
                coreUtilsendPost(url, data.field, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                });
            } else {

                let url = ctx + "sysUserRole/update";
                coreUtilsendPut(url, data.field, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                });
            }

            return false;
        });
    });

    //执行查询
    function search() {
        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                key: $("#key").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>